package com.power.project.dao.impl;

import com.alibaba.fastjson2.JSON;
import com.power.base.dao.OffersetTransformers;
import com.power.project.dao.Ww_userDaoCustom;
import com.power.project.model.Ww_user;
import com.power.project.model.dto.UserDTO;
import com.power.project.model.pageInfo.UserQueryPageInfo;
import com.power.project.model.vo.PasswordVO;
import com.power.project.utils.PageHelperUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;

/**
 * 持久层实现(hibernate)
 */

@Repository
public class Ww_userDaoImpl implements Ww_userDaoCustom<Ww_user, Serializable> {

    public static final String BLANK_STRING = " ";

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Page<UserDTO> findAll(UserQueryPageInfo pageInfo, Pageable pageable) {
        StringBuilder dataSql = new StringBuilder(
                "select wu.userid,wu.name,wu.avatar,wu.mobile,wu.maindepartment " +
                        "from ww_user wu\n" +
                        "         left join ha_user hu on wu.userid = hu.userid and hu.deleted = 0\n" +
                        "         left join ww_depart_user wdu on wu.userid = wdu.wwuserid and wdu.tenantid = :tenantid\n" +
                        "         left join ww_usertag wut on wu.userid = wut.wwuserid and wut.tenantid = :tenantid\n" +
                        "         left join ww_tag wt on wut.tagid = wt.tagid and wt.tenantid = :tenantid").append(BLANK_STRING);
        StringBuilder countSql = new StringBuilder(
                "select 1 " +
                        "from ww_user wu\n" +
                        "         left join ha_user hu on wu.userid = hu.userid and hu.deleted = 0\n" +
                        "         left join ww_depart_user wdu on wu.userid = wdu.wwuserid and wdu.tenantid = :tenantid\n" +
                        "         left join ww_usertag wut on wu.userid = wut.wwuserid and wut.tenantid = :tenantid\n" +
                        "         left join ww_tag wt on wut.tagid = wt.tagid and wt.tenantid = :tenantid").append(BLANK_STRING);

        StringBuilder whereSql = new StringBuilder("where hu.userid is not null").append(BLANK_STRING);

        StringBuilder orderSql = new StringBuilder("order by wu.userid").append(BLANK_STRING);

        StringBuilder groupSql = new StringBuilder("group by wu.userid, wu.name, wu.avatar, wu.mobile, wu.maindepartment").append(BLANK_STRING);

        if (StringUtils.isNotEmpty(pageInfo.getDepartmentId())) {
            whereSql.append("and wdu.departid = :departId").append(BLANK_STRING);
        }
        if (StringUtils.isNotEmpty(pageInfo.getTagId())) {
            whereSql.append("and wt.tagid = :tagId").append(BLANK_STRING);
        }
        if (StringUtils.isNotEmpty(pageInfo.getName())) {
            whereSql.append("and wu.name like concat('%',:name,'%')").append(BLANK_STRING);
        }

        dataSql.append(whereSql).append(groupSql).append(orderSql);
        countSql.append(whereSql).append(groupSql);
        String countSql1 = String.format("select count(1) from (%s) tmp", countSql);

        //创建本地sql查询实例
        Query dataQuery = entityManager.createNativeQuery(dataSql.toString());
        Query countQuery = entityManager.createNativeQuery(countSql1);

        dataQuery.setParameter("tenantid", pageInfo.getTenantId());
        countQuery.setParameter("tenantid", pageInfo.getTenantId());

        //设置参数
        if (StringUtils.isNotEmpty(pageInfo.getDepartmentId())) {
            dataQuery.setParameter("departId", pageInfo.getDepartmentId());
            countQuery.setParameter("departId", pageInfo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(pageInfo.getTagId())) {
            dataQuery.setParameter("tagId", pageInfo.getTagId());
            countQuery.setParameter("tagId", pageInfo.getTagId());
        }
        if (StringUtils.isNotEmpty(pageInfo.getName())) {
            dataQuery.setParameter("name", pageInfo.getName());
            countQuery.setParameter("name", pageInfo.getName());
        }

        return PageHelperUtil.getPage(pageable, dataQuery, countQuery, UserDTO.class);
    }

//    @Override
//    public Page<UserDTO> findAllByDepart(UserQueryPageInfo pageInfo, Pageable pageable) {
//        return findAll(pageInfo, pageable);
//    }
//
//    @Override
//    public Page<UserDTO> findAllByTag(UserQueryPageInfo pageInfo, Pageable pageable) {
//        return findAll(pageInfo, pageable);
//    }

    @Override
    public List<PasswordVO> queryUserPassword(String userid) {
        String sql = "select a.userid, b.password from ww_user a left join ha_user b on a.userid = b.userid where a.userid = ?";
        Query query = entityManager.createNativeQuery(sql);
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        query.setParameter(1, userid);
        List result = query.getResultList();
        return JSON.parseArray(JSON.toJSONString(result), PasswordVO.class);
    }
}